package com.zhongke.cotrun.mapper;

import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntity;
import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntityVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 创新活动表
 * 
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2019-11-15 14:59:00
 */
public interface InnovationActivitiesMapper {
    /**
    * 新增
    * @param innovationActivitiesEntity
    * @return
    */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into innovation_activities " +
            "(activities_topic,activities_content,activities_type,activities_industry,activities_mode,organizers,contractors,co_organizers,start_time,end_time,province_id,city_id,district_id,address,img_addrs,create_time,contact,phone_number,reg_end_time,reg_fee,reg_number,is_fee,words,create_id)" +
            " values (#{activitiesTopic},#{activitiesContent},#{activitiesType},#{activitiesIndustry},#{activitiesMode},#{organizers},#{contractors},#{coOrganizers},#{startTime},#{endTime}," +
            "#{provinceId},#{cityId},#{districtId},#{address},#{imgAddrs},now(),#{contact},#{phoneNumber},#{regEndTime},#{regFee},#{regNumber},#{isFee},#{words},#{createId})")
    public boolean insert(InnovationActivitiesEntity innovationActivitiesEntity);
    /**
    * 逻辑删除
    * @return
    */
    @Update("update innovation_activities set del_flag = 1 where id= #{id} and approval_status <> 1")
    public boolean deleteByID(@Param("id") Integer id);
    /**
     * 批量逻辑删除
     * @return
     */
    @Update("update innovation_activities set del_flag = 1 where id in ('${ids}') and approval_status <> 1;")
    public boolean deleteBatch(@Param("ids") String ids);
    /**
    * 彻底删除
    * @return
    */
    @Delete("delete from innovation_activities where id= #{id};")
    public boolean realDeleteByID(@Param("id") Integer id);
    /**
    * 修改
    * @param innovationActivitiesEntity
    * @return
    */
    @Update("update innovation_activities set activities_topic=#{activitiesTopic},activities_content=#{activitiesContent},activities_type=#{activitiesType}," +
            "activities_industry=#{activitiesIndustry},activities_mode=#{activitiesMode},organizers=#{organizers}," +
            "co_organizers=#{coOrganizers},start_time=#{startTime},end_time=#{endTime},province_id=#{provinceId},city_id=#{cityId},district_id=#{districtId}," +
            "address=#{address},img_addrs=#{imgAddrs},status=#{status},contact=#{contact},phone_number=#{phoneNumber}," +
            "reg_end_time=#{regEndTime},reg_fee=#{regFee},reg_number=#{regNumber},is_fee=#{isFee},words=#{words} where id = #{id}")
    public boolean updateByID(InnovationActivitiesEntity innovationActivitiesEntity);

    /**
     * 更新状态
     * @param id
     * @param status
     * @return
     */
    @Update("update innovation_activities set status = #{status} where id= #{id};")
    public boolean updateStatus(@Param("id") Integer id, @Param("status") Integer status);
    /**
    * 根据id查找
    * @return
    */
    @Select("select id,activities_topic,activities_content,activities_type,activities_industry,activities_mode,organizers,contractors,co_organizers,start_time,end_time," +
            "province_id,city_id,district_id,address,img_addrs,status,create_time,update_time,del_flag,contact,phone_number,reg_end_time," +
            "reg_fee,reg_number,is_fee,words,approval_status,approval_opinion,approval_user,approval_time,close_reason,create_id " +
            "from innovation_activities where id = #{id} limit 1")
    public InnovationActivitiesEntityVo selectByID(@Param("id") Integer id);
    /**
    * 根据条件查找(sql在mapper文件)
    * @param innovationActivitiesEntity
    * @return
    */
    public List<InnovationActivitiesEntityVo> search(InnovationActivitiesEntity innovationActivitiesEntity);

    /**
     * 根据id查询活动能否报名(未删除且阶段处于报名中)
     * @param id
     * @return
     */
    @Select("select count(id) from innovation_activities where del_flag=0 and status =0 and id = #{id}")
    int enbaleRegistration(@Param("id") Integer id);

    @Select("SELECT count(0) as counts,DATE_FORMAT(create_time,#{format}) activitiesTopic FROM `innovation_activities` where del_flag = 0 and status!=3 group by activitiesTopic limit #{size}")
    List<InnovationActivitiesEntityVo> activitiesRiseTrend(String format, Integer size);

    @Update("update innovation_activities set status = #{status},close_reason = #{closeReason} where id= #{id};")
    boolean updateItemStatus(Integer id, Integer status, String reason);

    @Update("update innovation_activities set approval_status = #{approvalStatus},approval_opinion = #{approvalOpinion},approval_user=#{userName},approval_time = now() where id= #{id}")
    boolean updateApprovalStatus(int approvalStatus, String approvalOpinion, Integer id,String userName);

    @Select("SELECT ia.id,pmi.id pay_mall_info_id,ia.activities_topic,ia.start_time,ia.province_name,ia.city_name,ia.district_name,ia.address,ia.reg_number,ia.is_fee,ia.reg_fee " +
            "FROM `pay_mall_info` pmi,innovation_activities_view ia " +
            "where pmi.is_enable =1 and pmi.del_flag = 0 and ia.`status` < 2 and ia.approval_status = 1 and ia.del_flag = 0 and pmi.main_id = ia.id and ia.id = #{id} limit 1")
    InnovationActivitiesEntityVo getSignUpInfo(@Param("id")Integer id);

    /**
     * 定时处理活动状态
     * @return
     */
    @Update("update innovation_activities set status = 2 where status = 1 and reg_end_time < now()")
    Integer cronJobsUpdateState2();

    /**
     * 下线
     * @param id
     * @return
     */
    @Update("update innovation_activities set approval_status = 4,status=2 where id= #{id} and approval_status = 1")
    boolean closeActivities(Integer id);


    @Select("select count(0) from innovation_activities_view where approval_status = 0 and del_flag = 0")
    int queryUnApprovalCounts();

    List<InnovationActivitiesEntityVo> queryOrderForCenter(InnovationActivitiesEntityVo innovationActivitiesEntity);
}
